package com.apotik.legenda.frameinternal;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.border.MatteBorder;

import com.apotik.legenda.custom.CustomComboboxModel;
import com.apotik.legenda.dao.ShiftDao;
import com.apotik.legenda.dao.UserDao;
import com.apotik.legenda.frame.PosFrame;
import com.apotik.legenda.model.Shift;
import com.apotik.legenda.model.User;
import com.apotik.legenda.util.CalendarUtil;

public class SelectKasirInternalFrame extends JInternalFrame implements ActionListener, KeyListener {
	private static final long serialVersionUID = 1L;

	private JPanel panel;
	private JButton btnSubmit;
	private JButton btnCancel;
	
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxKasir;
	
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxShift;
	
	private JFrame mainFrame;
	private CustomComboboxModel modelKasir = null;
	private CustomComboboxModel modelShift = null;
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public SelectKasirInternalFrame(JFrame mainFrame) {
		super("Select Cashier", false, true, false, true);
		setSize(355, 190);
		setVisible(true);
		addKeyListener(this);
		
		this.mainFrame = mainFrame;
		
		panel = new JPanel();
		panel.setLayout(null);
		getContentPane().add(panel);
		
		JLabel lblKasir = new JLabel("Kasir");
		lblKasir.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblKasir.setBounds(33, 35, 63, 18);
		panel.add(lblKasir);
		
		comboBoxKasir = new JComboBox();
		comboBoxKasir.setFont(new Font("Tahoma", Font.PLAIN, 14));
		comboBoxKasir.setBounds(110, 34, 181, 20);
		CustomComboboxModel modelCashier = comboboxModelCashier();
		comboBoxKasir.setModel(modelCashier);
		panel.add(comboBoxKasir);
		
		JLabel lblShift = new JLabel("Shift");
		lblShift.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblShift.setBounds(33, 65, 63, 18);
		panel.add(lblShift);
		
		comboBoxShift = new JComboBox();
		comboBoxShift.setFont(new Font("Tahoma", Font.PLAIN, 14));
		comboBoxShift.setBounds(110, 65, 181, 20);
		CustomComboboxModel modelShift = comboboxModelShift();
		comboBoxShift.setModel(modelShift);
		panel.add(comboBoxShift);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnSubmit.setBounds(110, 97, 83, 23);
		btnSubmit.addActionListener(this);
		btnSubmit.addKeyListener(this);
		panel.add(btnSubmit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnCancel.setBounds(204, 97, 87, 23);
		btnCancel.addActionListener(this);
		btnCancel.addKeyListener(this);
		panel.add(btnCancel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.LIGHT_GRAY));
		panel_1.setBounds(10, 11, 319, 132);
		panel.add(panel_1);
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private CustomComboboxModel comboboxModelCashier(){
		
		UserDao dao = new UserDao();
		List<User> cashiers = dao.loadCashiersByActive(true);
		
		Vector vector = new Vector();

		Vector v = new Vector();
		v.add("--- Pilih Kasir ---");
		v.add(null);
		vector.add(v);
		
		for(User user: cashiers){
			v = new Vector();
			v.add(user.getFullName());
			v.add(user);
			
			vector.add(v);
		}
		
		CustomComboboxModel model = new CustomComboboxModel(vector);
		this.modelKasir = model;
		
		return model;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private CustomComboboxModel comboboxModelShift(){
		
		Vector vector = new Vector();

		Vector v = new Vector();
		v.add("--- Pilih Shift ---");
		v.add(null);
		vector.add(v);
		
		ShiftDao dao = new ShiftDao();
		List<Shift> list = dao.loadAll();
		
		for(Shift obj: list){
			String timeStart = CalendarUtil.dateToString_HHMM(obj.getStartTime());
			String timeFinish = CalendarUtil.dateToString_HHMM(obj.getFinishTime());
			
			v = new Vector();
			v.add(obj.getShiftName()+" ("+timeStart+"-"+timeFinish+")");
			v.add(obj);
			
			vector.add(v);
		}
		
		CustomComboboxModel model = new CustomComboboxModel(vector);
		this.modelShift = model;
		
		return model;
	}
	
	private void closeInternalFrame(){
		this.dispose();
		this.setVisible(false);
	}
	
	private void btnSubmitAction(){
		if(comboBoxKasir.getSelectedIndex()==0){
			JOptionPane.showMessageDialog(null, "User Kasir belum dipilih", "Exception", JOptionPane.ERROR_MESSAGE);
			comboBoxKasir.requestFocus();
			return;
		}
		
		if(comboBoxShift.getSelectedIndex()==0){
			JOptionPane.showMessageDialog(null, "Shift belum dipilih", "Exception", JOptionPane.ERROR_MESSAGE);
			comboBoxShift.requestFocus();
			return;
		}
		
		User user = (User) this.modelKasir.getModelDataAt(comboBoxKasir.getSelectedIndex());
		Shift shift = (Shift) this.modelShift.getModelDataAt(comboBoxShift.getSelectedIndex());

		PosFrame frame = new PosFrame(mainFrame, user, shift);
		frame.getFrame().setVisible(true);
		
		this.setVisible(false);			
		mainFrame.setVisible(false);
	}
	
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		Object obj = ae.getSource();
		
		if(obj==btnCancel){
			closeInternalFrame();
		}else if(obj==btnSubmit){
			btnSubmitAction();
		}
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		int keyCode = ke.getKeyCode();
		if(keyCode==KeyEvent.VK_ESCAPE){
			closeInternalFrame();
		}
		Object source = ke.getSource();
		
		if(source==btnSubmit && keyCode==KeyEvent.VK_ENTER){
			btnSubmitAction();
		}else if(source==btnCancel && keyCode==KeyEvent.VK_ENTER){
			closeInternalFrame();
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub	
	}
	
}
